<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({ name: 'menu-create' })"
          >+新增目录</el-button
        >
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="date" label="编号" width="180" type="index">
        </el-table-column>
        <el-table-column prop="name" label="菜单名称" width="180">
        </el-table-column>
        <el-table-column prop="level" label="菜单级数"> </el-table-column>
        <el-table-column prop="icon" label="前端图标"> </el-table-column>
        <el-table-column prop="orderNum" label="排序"> </el-table-column>
        <el-table-column label="操作">
          <!-- 需要注意的是 -->
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getAllMenu, deleteMenu } from "@/services/menu";
import { Message } from "element-ui";

export default {
  // 导出当前组件的配置对象
  name: "Menu",
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
    };
  },

  created() {
    this.loadAllMenus();
  },

  methods: {
    // 获取目录列表
    async loadAllMenus() {
      const { data } = await getAllMenu();
      console.log(data);
      if (data.code === "000000") {
        this.tableData = data.data;
      }
    },
    handleEdit(rowData) {
      // 设置跳转
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: rowData.id
        }
      })
    },

    handleDelete(rowData) {
      console.log(rowData);
      // 删除的确认提示。
      this.$confirm("确认删除吗？, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 进行删除操作。
          const { data } = await deleteMenu(rowData.id);

          if (data.code === "000000") {
            Message.success(data.mesg);
            // ()表示调用。 this.表示当前组件。
            this.loadAllMenus();
          }
        })
        .catch(() => {
          //
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
